package com.smalls.newvideotwo.view;

import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.MeasureUtil;
import com.smalls.newvideotwo.R;

import java.text.NumberFormat;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Created by hs on 2018/6/5.
 */

public class FloatWindows {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + FloatWindows.class.getSimpleName():FloatWindows.class.getSimpleName();
    private final int IN_PROGRESS = 5;
    private final int IN_CLOSE = 6;
    private boolean initFloat = false;
    private boolean isAdd = false;

    private WindowManager mWindowManager;
    private View mViewFloat;
    private LoadTextView mTxt;
    private LoadProgressBarView mPro;
    private NumberFormat numberFormat;
    private ProgressPojo pojo;
    private WindowManager.LayoutParams params;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case IN_PROGRESS:
                    ///Log.d(TAG, "IN_PROGRESS isAttachedToWindow  " + mViewFloat.isAttachedToWindow());
                    if (pojo.progress == pojo.total) {
                        clear2Window();
                    } else {
                        add2Window();
                    }
                    if (mPro.getMax() != pojo.total) {
                        mPro.setMax(pojo.total);
                    }
                    mPro.setProgress(pojo.progress);
                    mTxt.setText(pojo.percent + "%");
                    break;
                case IN_CLOSE:
                    clear2Window();
                    break;
            }
        }
    };

    public void initFloat() {
        mWindowManager = (WindowManager) NewDTVApp.getApp().getSystemService(NewDTVApp.getApp().WINDOW_SERVICE);
        params = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST);
        numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(1);
        pojo = new ProgressPojo();
        LayoutInflater layoutInflater = (LayoutInflater) NewDTVApp.getApp().getSystemService(LAYOUT_INFLATER_SERVICE);
        mViewFloat = layoutInflater.inflate(R.layout.float_progress, null);
        mTxt = (LoadTextView) mViewFloat.findViewById(R.id.float_p);
        mPro = (LoadProgressBarView) mViewFloat.findViewById(R.id.float_progressbar);
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.width = (int) MeasureUtil.getWidthSize(400);
        params.height = (int) MeasureUtil.getHeightSize(100);
        params.gravity = Gravity.TOP;
        params.format = PixelFormat.RGBA_8888;
        params.x = (int) MeasureUtil.getWidthSize(730);
        params.y = (int) MeasureUtil.getHeightSize(40);
    }

    public void onProgress(long progress, long total) {
        //Log.d(TAG, "onProgress progress="+progress+" total="+total);
        pojo.progress = (int) progress;
        pojo.total = (int) total;
        pojo.percent = numberFormat.format((float) progress / total * 100);
        mHandler.sendEmptyMessage(IN_PROGRESS);
    }

    public void onFinish(int arg0) {
        mHandler.sendEmptyMessage(IN_CLOSE);
    }

    public void releaseFloat() {
        mHandler.removeCallbacksAndMessages(null);
        clear2Window();
//        mWindowManager = null;
//        params = null;
//        numberFormat = null;
//        pojo = null;
//        mViewFloat = null;
//        mTxt = null;
//        mPro = null;
    }

    private void add2Window() {
        if (!isAdd && mWindowManager != null && mViewFloat != null && params != null) {
            mWindowManager.addView(mViewFloat, params);
            isAdd = true;
        }
    }

    private void clear2Window() {
        if (isAdd && mWindowManager != null && mViewFloat != null && params != null) {
            mWindowManager.removeView(mViewFloat);
            isAdd = false;
        }
    }

    final class ProgressPojo {
        public int progress;
        public int total;
        public String percent = "0";
    }
}
